/***************************************************************************************************
  Copyright(C)   2011-9-5
  All rights reserved.
  File Name: updatesocket.h

  Description:更新模块的业务实际处理模块


  ***************************************************************************************************/

#ifndef UPDATESOCKET_H
#define UPDATESOCKET_H

#include <QTcpSocket>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QObject>
#include <QCryptographicHash>

#include "agreementmsg.h"
#include "recepkgmanage.h"
#include "environment.h"
#include "update/updatexmlparser.h"


#define VERSIONFILENAME     "updateinfo"

extern QString g_installPath;

class UpdateSocket : public QTcpSocket
{
    Q_OBJECT
public:
    UpdateSocket(int socketId, int state = 0, QObject *parent = 0);

private:
    void init();
    ReceInfoStruct& initReceInfoStruct(ReceInfoStruct &receInfoStruct);

    void sendConnectSuccess();

    void processReceData(const QByteArray &receData);
    void writeDataToFile(PIDS_FilePkg& filePkg);
    void receFileFinish(const PIDS_SendFileFish& fileFinish);

    void applyLostFile(const int receInfoNum);
    void applyGetFile(const QString& fileName);
    bool checkUnfinishFile();

    void sendUpdateSuccess();

    void sendUpdateErr(quint8 err);


    unsigned short checkSum(char *buffer, int size);


//    void applyGetUnfinishFile(const QString& fileName, const int infoNum);

signals:
    void disconnectSignal();
    void receUpdateState(const int state);

private:
    QByteArray              m_byteArray;
    QList<ReceInfoStruct>   m_receFileInfoList;
    UpdateInfo              m_updateInfo;
    QStringList             m_hadDLFiles;

    int                     m_curInfoNum;       //
    long                    m_receSize;

private slots:
    void receDataFromServer();
    void receData();
    void disConnectSlot();

    void receXmlParser(int err);

};

#endif // UPDATESOCKET_H
